<?php
// +----------------------------------------------------------------------
// | B5YiiCMF
// +----------------------------------------------------------------------
// | Author: 李恒 <357145480@qq.com>
// +----------------------------------------------------------------------
namespace h5\controllers;

use common\helpers\commonApi;
use yii\helpers\Url;
use yii\web\Controller;
use Yii;

/**
 * PC端通用父类
 * Class BaseController
 * @package pc\controllers
 */
class BaseController extends Controller
{
    public $view_group='';
    /**
     * 构造函数
     * BaseController constructor.
     */
    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        $this->initSystemConst();
    }

    /**
     * 初始化系统常量
     */
    private function initSystemConst()
    {
        defined('IS_GET') or define('IS_GET', Yii::$app->request->isGet);
        defined('IS_POST') or define('IS_POST', Yii::$app->request->isPost);
        defined('IS_AJAX') or define('IS_AJAX', Yii::$app->request->isAjax);
        defined('IS_PJAX') or define('IS_PJAX', Yii::$app->request->isPjax);
        defined('IS_PUT') or define('IS_PUT', Yii::$app->request->isPut);
        defined('IS_DELETE') or define('IS_DELETE', Yii::$app->request->isDelete);
        defined('REQUEST_METHOD') or define('REQUEST_METHOD', strtoupper(Yii::$app->request->method));
        defined('PAGE_LIMIT') or define('PAGE_LIMIT', 10);
        defined('MODULES_NAME') or define('MODULES_NAME', 'h5');
        defined('IMG_URL') or define('IMG_URL',Yii::getAlias('@appweb'));
    }

    /**
     * 定义控制器名和方法名，并分配到视图，登陆检测，权限检测
     * @param \yii\base\Action $action
     * @return bool
     * @throws \yii\web\BadRequestHttpException
     */
    public function beforeAction($action)
    {
        if(parent::beforeAction($action)){
            $controller=Yii::$app->controller->id;
            $action=Yii::$app->controller->action->id;
            defined('CONTROLLER_NAME') or define('CONTROLLER_NAME', $controller);
            defined('ACTION_NAME') or define('ACTION_NAME', $action);
            return true;
        }
        return false;
    }
    /**
     * beforeAction发生错误时的跳转
     * @param string $msg
     * @param string $url
     * @param int $code
     * @return bool
     */
    public function resError($msg='',$url='',$code=-1){
        $msg=$msg?:'发生错误';
        if(Yii::$app->request->isGet && !Yii::$app->request->isAjax){
            if($url){
                $url=is_array($url)?$url:(array)$url;
            }else{
                $url=['public/fail','msg'=>$msg];
            }
            Yii::$app->response->redirect($url);
        }else{
            $url=$url?Url::toRoute($url):'';
            Yii::$app->response->data =commonApi::message($msg,false,[],$code,$url);
        }
        return false;
    }
    /**
     * 跳转到错误页
     * @param string $msg
     * @param int $code
     * @return string
     */
    public function toError(string $msg = '哎呦，您访问的页面不存在(⋟﹏⋞)', int $code = 400)
    {
        $data = ['msg' => $msg, 'code' => $code];
        return $this->renderPartial('/public/fail', $data);
    }

    /**
     * 自动加载视图
     * @param string $view
     * @param array $params
     * @return string
     */
    public function render($view='', $params = [])
    {
        Yii::$app->response->format=\yii\web\Response::FORMAT_HTML;
        if(empty($view)){
            $view=strtolower(ACTION_NAME);
            if($this->view_group){
                $view=$this->view_group.'/'.$view;
            }
        }
        return parent::render($view, $params); // TODO: Change the autogenerated stub
    }
    public function renderPartial($view='', $params = [])
    {
        if(empty($view)){
            $view=strtolower(ACTION_NAME);
            if($this->view_group){
                $view=$this->view_group.'/'.$view;
            }
        }
        return parent::renderPartial($view, $params); // TODO: Change the autogenerated stub
    }
}
